package com.top.service;

import com.top.dao.DAOFactory;
import com.top.dao.FoodDao;
import com.top.em.MenuEnum;
import com.top.em.TipEnum;
import com.top.entiy.Food;
import com.top.entiy.FoodCategory;
import com.top.utils.SoutUtil;

import java.util.List;
import java.util.Scanner;

/**
 * @Author:
 * @DateTime: 2023/12/8 22:54
 * @Description: TODO
 */
public class CategoryFoodService {

    public void index() {
        System.out.println("-------" + MenuEnum.CATEGORY_FOOD.getTip() + "--------");
        Scanner scanner = new Scanner(System.in);
        System.out.println(TipEnum.COMMON.getTip());

        //新增 修改 删除 菜品列表
        SoutUtil.printButton(MenuEnum.ADD, MenuEnum.EDIT, MenuEnum.DELETE, MenuEnum.LIST_FOOD);
        List<FoodCategory> all = DAOFactory.getCategoryFoodDAOInstance().findAll();
        System.out.println("-------菜品类别列表--------");
        System.out.println(new FoodCategory().getColumn());
        for (FoodCategory foodCategory : all) {
            System.out.println(foodCategory.toString());
        }
        String next = scanner.next();
        try {
            if (next.startsWith("A")) {
                add();
            } else if (next.startsWith("E")) {
                edit();
            } else if (next.startsWith("D")) {
                delete();
            } else if (next.startsWith("L")) {
                cList();
            } else if (next.startsWith("Q")) {
                new CommonService().index();
            } else {
                System.err.println("输入错误");
                index();
            }
        } catch (Exception e) {
            System.err.println("输入错误");
            index();
        }
    }

    private void cList() {
        System.out.println("-------" + MenuEnum.LIST_FOOD.getTip() + "--------");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入菜品类别ID以查看菜品类别下的菜品列表");
        int categoryId = scanner.nextInt();
        listByCategoryId(categoryId);
    }

    private void listByCategoryId(int categoryId) {
        Scanner scanner = new Scanner(System.in);
        FoodCategory foodCategory = DAOFactory.getCategoryFoodDAOInstance().findById(categoryId);
        if (foodCategory == null) {
            System.err.println("菜品类别不存在");
            cList();
        }
        List<Food> foodList = DAOFactory.getFoodDAOInstance().findAllByCategoryId(categoryId);
        System.out.println("------- " + foodCategory.getCategoryName() + " 菜品列表--------");
        System.out.println(new Food().getColumn());
        for (Food food : foodList) {
            System.out.println(food.toString());
        }
        System.out.println(TipEnum.COMMON.getTip());
        SoutUtil.printButton(MenuEnum.ADD, MenuEnum.DELETE);
        String next1 = scanner.next();
        try {
            if (next1.startsWith("A")) {
                addFood(categoryId);
            } else if (next1.startsWith("D")) {
                deleteFood(categoryId);
            } else if (next1.startsWith("Q")) {
                index();
            } else {
                System.err.println("输入错误");
                cList();
            }
        } catch (Exception e) {
            System.err.println("输入错误");
            cList();
        }
    }

    /**
     * 菜品删除
     */
    private void deleteFood(int categoryId) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的菜品id");
        int id = scanner.nextInt();
        Food food = DAOFactory.getFoodDAOInstance().findById(id);
        if (food == null) {
            System.err.println("菜品不存在");
            listByCategoryId(categoryId);
        }
        int category = food.getCategory();
        if (category != categoryId) {
            System.err.println("菜品类别不匹配");
            listByCategoryId(categoryId);
        }
        food.setCategory(0);
        boolean b = DAOFactory.getFoodDAOInstance().doUpdate(food);
        if (b) {
            System.out.println("菜品删除成功");
            listByCategoryId(categoryId);
        } else {
            System.err.println("菜品删除失败");
            listByCategoryId(categoryId);
        }
    }

    /**
     * 菜品添加
     */
    private void addFood(int categoryId) {
        System.out.println("-------" + MenuEnum.ADD.getTip() + "--------");
        System.out.println("-----------全部菜品列表-----------");
        List<Food> all = DAOFactory.getFoodDAOInstance().findAll();
        System.out.println(new Food().getColumn());
        for (Food food : all) {
            System.out.println(food.toString());
        }
        Scanner scanner = new Scanner(System.in);
        FoodCategory foodCategory = DAOFactory.getCategoryFoodDAOInstance().findById(categoryId);
        if (foodCategory == null) {
            System.err.println("菜品类别不存在");
        }
        String categoryName = foodCategory.getCategoryName();
        System.out.println("输入菜品ID将菜品添加到类别 " + categoryName + " 中:");
        int foodId = scanner.nextInt();
        Food food = DAOFactory.getFoodDAOInstance().findById(foodId);
        if (food == null) {
            System.err.println("菜品不存在");
            listByCategoryId(categoryId);
        }
        food.setCategory(categoryId);
        boolean b = DAOFactory.getFoodDAOInstance().doUpdate(food);
        if (b) {
            System.out.println("添加成功");
        } else {
            System.err.println("添加失败");
        }
        listByCategoryId(categoryId);
    }


    private void delete() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的菜品类别ID");
        String next = scanner.next();
        String[] split = next.split(",");
        String id = split[0];
        boolean b = DAOFactory.getCategoryFoodDAOInstance().doRemove(Integer.valueOf(id));
        if (b) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
        index();
    }

    private void edit() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("清输入菜品类别信息进行修改 [ID,类别名称,排序值]");
        String next = scanner.next();
        String[] split = next.split(",");
        String id = split[0];
        String name = split[1];
        String sort = split[2];
        FoodCategory byName = DAOFactory.getCategoryFoodDAOInstance().findByName(name);
        if (byName != null && byName.getId() != null && !byName.getId().equals(Integer.valueOf(id))) {
            System.out.println("菜品类别已存在");
        } else {
            FoodCategory categoryFood = new FoodCategory();
            categoryFood.setId(Integer.valueOf(id));
            categoryFood.setCategoryName(name);
            categoryFood.setSort(Integer.parseInt(sort));
            boolean b = DAOFactory.getCategoryFoodDAOInstance().doUpdate(categoryFood);
            if (b) {
                System.out.println("更新成功");
            } else {
                System.out.println("更新失败");
            }
        }
        index();
    }

    private void add() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入菜品类别以创建 [类别名称,排序值]");
        String next = scanner.next();
        String[] split = next.split(",");
        String name = split[0];
        String sort = split[1];

        //查询菜品列表是否存在
        if (DAOFactory.getCategoryFoodDAOInstance().findByName(name) != null) {
            System.out.println("菜品类别已存在");
        } else {
            FoodCategory categoryFood = new FoodCategory();
            categoryFood.setCategoryName(name);
            categoryFood.setSort(Integer.parseInt(sort));
            boolean b = DAOFactory.getCategoryFoodDAOInstance().doCreate(categoryFood);
            if (b) {
                System.out.println("创建成功");
            } else {
                System.out.println("创建失败");
            }
        }
        index();
    }
}
